﻿using System.Collections;
using System.Linq;
using Moq;
using Myotragus.Data.Containers;
using Myotragus.Data.Criteria;
using Myotragus.Data.Repositories;
using Myotragus.Data.Tests.Domain;
using NUnit.Framework;

namespace Myotragus.Data.Tests.Tests.Repository
{
	[TestFixture]
	public class WriteRepositoryFixtures
	{
		[Test]
		public void SaveAndDelete()
		{
			var entities = new Mock<IEntitiesContainer>();
			var queryBuilder = new Mock<IWriteQueryBuilder>();

			var entityMock = new Mock<ITestEntity>();
			var resultType = entityMock.Object.GetType();
			var entity = entityMock.Object;

			entities.Setup(m => m.ResolveType(typeof(ITestEntity))).Returns(resultType);

			queryBuilder.Setup(m => m.Save(It.Is<IEnumerable>(t =>
				t.Cast<ITestEntity>().Count() == 1 &&
				t.Cast<ITestEntity>().Contains(entity))));

			queryBuilder.Setup(m => m.Delete(It.Is<IEnumerable>(t =>
				t.Cast<ITestEntity>().Count() == 1 &&
				t.Cast<ITestEntity>().Contains(entity))));

			var repo = new WriteRepository<ITestEntity, int>(entities.Object, queryBuilder.Object);

			repo.Save(entity);
			repo.Delete(entity);

			entities.VerifyAll();
			queryBuilder.VerifyAll();
		}
	}
}